<template>
  <div class="all">
    <el-card class="box-card">
      <span slot="header" class="title">设置新密码</span>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        style="width: 320px; margin: 20px auto"
        class="demo-ruleForm"
      >
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            style="width: 110px"
            @click="submitForm('ruleForm')"
            >提 交</el-button
          >
          <el-button @click="cancle()" style="width: 110px">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template> 
<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" },
        {pattern: /^(\w){6,18}$/, message: '只能输入6-18个字母、数字、下划线'}],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var id = sessionStorage.getItem("Id");
          this.$axios.get("/password", {
            params:{
              user_id: id,
              user_password: this.ruleForm.pass,
            }
          })
          .then((res) => {
            if (res.data.status == "1000") {
              this.$router.push("/");
            } else {
              alert(res.data.messages);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cancle(){
      this.$router.push("/Forget");
    }
  },

}

</script>

<style>
.all {
  border: 1px solid white;
  height: 700px;
  background-image: url("../assets/beijing.jpg");
  background-size: 100% 100%;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.title {
  text-align: center;
  font-size: 24px;
  color: rgba(50, 50, 50, 0.8);
}
.box-card {
  background-color: aliceblue;
  border-radius: 10px;
  width: 480px;
  margin: 10% auto;
}
</style>